<?php 

class BlogController extends AuthController{
	private $db;
	public function __init()
	{
		$this->db=new BlogModel;
	}
	//显示博客列表
	public function index()
	{
		$data=$this->db->getAll();
		$this->assign('data',$data['data']);
		$this->assign('page',$data['page']);
		$this->display('index.html');
	}

	//添加博客
	public function add()
	{
		if (IS_POST) {
			if ($this->db->addBlog()) {
				$this->success('添加成功',__CONTROLLER__);
				return true; 
			}else{
				$this->error($this->db->error);
				return false;
			}
		}else{
			$cate=new CategoryModel;
			$catedb=$cate->getlist();
			$this->assign('cate',$catedb);
			$this->display('add.html');
		}	
	}

	//修改博客
	public function edit()
	{
		if (IS_POST) {
			if ($this->db->editBlog()) {
				$this->success('修改成功',__CONTROLLER__);
				return true;
			}else{
				$this->error($this->db->error);
				return false; 
			}
		}else{
			//获得对应的博文
			$blogdb=$this->db->where("bid={$_GET['bid']}")->find();
			//获得所有栏目
			$cate=new CategoryModel;
			$catedb=$cate->getlist();
			//当前博客为选中状态
			foreach ($catedb as $key => $value) {
				$catedb[$key]['selected']=($_GET['cid']==$value['cid'])?" selected=''":'';
			}
			$this->assign('cate',$catedb);
			$this->assign('blog',$blogdb);
			$this->display('edit.html');	
		}	
	}

	//删除博客
	public function delete()
	{
		if ($this->db->delBlog($_GET['bid'])) {
			$this->success('删除成功',__CONTROLLER__);
		}else{
			$this->error($this->db->error);
		}
	}



}

 ?>